<?php

namespace app\admin\controller\system;

use albert\services\CacheService;
use albert\services\JsonService;
use albert\utils\File;
use app\admin\controller\AuthController;
use think\facade\Db;

/**
 * 清除管理控制器
 * Class ClearController
 * @package app\admin\controller\system
 */
class ClearController extends AuthController
{
    /**
     * 入口展示页
     * Author: Albert3306
     * Date: 2021/7/14
     * @return string
     */
    public function index(): string
    {
        return $this->fetch('system/clear');
    }

    /**
     * 清除缓存
     * Author: Albert3306
     * Date: 2021/7/14
     * @return mixed
     */
    public function clearCache()
    {
        $root       = app()->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR;
        $adminRoute = $root . 'admin';
        $apiRoute   = $root . 'api';
        $cacheRoute = $root . 'cache';
        $cache      = [];

        if (is_dir($adminRoute))
            $cache[$adminRoute] = scandir($adminRoute);
        if (is_dir($apiRoute))
            $cache[$apiRoute] = scandir($apiRoute);
        if (is_dir($cacheRoute))
            $cache[$cacheRoute] = scandir($cacheRoute);

        foreach ($cache as $p => $list) {
            foreach ($list as $file) {
                if (!in_array($file, ['.', '..', 'log', 'schema', 'route.php'])) {
                    $path = $p . DIRECTORY_SEPARATOR . $file;
                    File::deleteDir($path);
                }
            }
        }
        CacheService::clear();
        return app('json')->successful('清除成功！');
    }

    /**
     * 清除日志
     * Author: Albert3306
     * Date: 2021/7/14
     * @return mixed
     */
    public function clearLog()
    {
        $root = app()->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR;
        File::deleteDir($root . 'admin' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR);
        File::deleteDir($root . 'api' . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR);
        File::deleteDir($root . 'log' . DIRECTORY_SEPARATOR);

        return app('json')->successful('清除成功!');
    }

    /**
     * 清除数据
     * Author: Albert3306
     * Date: 2021/8/25
     * @param $type
     * @return string
     */
    public function clearData($type): string
    {
        switch ($type) {
            case 2: // 清除临时附件
                break;
            case 3: // 清除用户数据
                break;
            case 4: // 清除内容分类
                break;
            case 5: // 清除所有附件
                self::clearTableData('system_attachment');
                self::clearTableData('system_attachment_category');
                File::deleteDir(app()->getRootPath() . 'public' . DIRECTORY_SEPARATOR . 'uploads');
                break;
            case 6: // 清除系统记录
                self::clearTableData('system_log');
                break;
        }
        return JsonService::successful('清除数据成功!');
    }

    /**
     * 清空数据库表数据
     * Author: Albert3306
     * Date: 2021/9/2
     * @param $table_name
     */
    public function clearTableData($table_name)
    {
        $table_name = config('database.connections.' . config('database.default'))['prefix'] . $table_name;
        @Db::execute('TRUNCATE TABLE ' . $table_name);
    }
}